var toast = require("../../../components/toast/toast.js")
var app = getApp();
var pageData = {
    data: {
        orderPayAmount: 0,
        list: [],
        couponId: "",
        violationIds: "",
	      platform: "iPhone"
    },

    onLoad: function (options) {
        console.log('参数',options)
        wx.setNavigationBarTitle({ title: "订单确认" });
        let unidList = options.unidList;
        let backendIdList = options.backendIdList;
        let carId = options.carId;
        // console.log(unidList);
        if (unidList != undefined) {
            this.setData({
                "violationIds": unidList,
                "backendIdList": backendIdList,
                "carId": carId
            })
        }

        this.getIllegalListByUnId();
        this.prepareUserOrder();
	    if (app.isAndroid()) {
		    this.setData({platform: "android"});
	    }
    },
    onShow: function () {

    },
    onReady: function () {
        // Do something when page ready.
    },
    getIllegalListByUnId: function (e) {
        var carId = this.data.carId;
        var violationIds = this.data.violationIds;

        var param = {
          api: 'Service.UcPecc.PeccService.GetPeccDetailById',
	        url: '/wx/carViola/getPeccDetailById.ajax',
	        para: {
              "violationIds": violationIds,  //'D3AD38A49AFC4AF9A03584BBE597B3FD,B1EB234628D44A718D9590DC6885A62C',  //
              "carId": this.data.carId  //'E491EC96A20347248E855E78FD51EBF7'  //
          }
        };

        app.requestSdkForNeedLogin({
            param: param,
            beforeSend: () => {
                //发送请求之前执行
                wx.showToast({
                    title: '请稍候',
                    icon: 'loading',
                    duration: 10000,
                    mask: true
                });
            },
            success: data => {
                if (data.successFlag) {
                    var illegalList = data.data;
                    var tsList = [];
                    var newLoation = "";
                    for (var i = 0; i < illegalList.length; i++) {
                        var location = illegalList[i].location;
                        var locationName = illegalList[i].locationName;
                        if (locationName == "") {
                            if (location.length > 5)
                                newLoation = location.substring(0, 5) + "...";
                            else
                                newLoation = location;
                        }
                        else
                            newLoation = locationName + "违章";
                        var tsItem = {
                            "carNumber": illegalList[i].carNumber,
                            "location": illegalList[i].location,
                             "occurTime": app.toYMDDate(illegalList[i].occurTime),
                            "fine": illegalList[i].fine,
                            "cooperPoundge": illegalList[i].cooperPoundge,
                            "pounDage": illegalList[i].pounDage,
                            "canProcessMsg": illegalList[i].canProcessMsg,
                            "canProcess": illegalList[i].canProcess,
                            "degree": illegalList[i].degree,
                            "other": illegalList[i].other,
                            "locationName": newLoation
                        };
                        tsList.push(tsItem);
                    }
                    this.setData({
                        "list": tsList
                    });
                    wx.hideToast();
                } else {
                    wx.hideToast();
                    toast.show(this, data.errormsg)
                }
            },
            complete: data => {

            }
        });
    },
    //订单试算
    prepareUserOrder: function (e) {
        var param = {
	        url: '/wx/carViola/prepareOrder.ajax',
	        para: {
                "carId": this.data.carId, 
                "violationIds": this.data.violationIds,  
                "backendIds": this.data.backendIdList,
                "couponId": this.data.couponId,
                "isUrgent": true,
                "phone": ""
            }
        };

        app.requestSdkForNeedLogin({
            param: param,
            beforeSend: () => {
               
            },
            success: data => {
                if (data.successFlag) {
                    this.setData({
                        "orderPayAmount": data.data.orderPayAmount
                    })
                } else {
                    toast.show(this, "订单试算失败")
                }
            },
            complete: data => {
                // wx.hideToast();
            }
        });
    },
    creatOrderInfo: function () {
        var that = this;
        var param = {
	        url: '/wx/carViola/addOrder.ajax',
	        para: {
            "carId":  this.data.carId,
            "violationIds": this.data.violationIds, 
            "couponId": this.data.couponId,
            "backendIds": this.data.backendIdList,
            "payType": 3,
            "isUrgent": true,
            "phone": ""
            }
        };

        app.requestSdkForNeedLogin({
            param: param,
            beforeSend: () => {
                //发送请求之前执行
                wx.showToast({
                    title: '请稍候',
                    icon: 'loading',
                    duration: 10000,
                    mask: true
                });
            },
            success: data => {
                if (data.successFlag) {
                  var paySng = data.data.wxPayInfo,
                      orderId = data.data.orderIds[0].id;
	                wx.requestPayment({
		                'timeStamp': paySng.timeStamp,
		                'nonceStr': paySng.nonceStr,
		                'package': paySng.packageName,
		                'signType':paySng.signType,
		                'paySign': paySng.paySign,
		                'success':function(result){
			                var param = {
				                url: '/wx/ed/payCallback.ajax',
				                para: {
					                "orderId": data.data.orderIds[0].id,   
					                "prepayId": data.data.payOrderId,
					                "platform": that.data.platform
				                }
			                };

			                app.requestSdkForNeedLogin({
                            param: param,
                            beforeSend: function(data){
                              //发送请求之前执行
                              wx.showToast({
                                title: '请稍候',
                                icon: 'loading',
                                duration: 10000,
                                mask: true
                              });
		                     },
                          "success": function (data){
	                          wx.redirectTo({url: '../paySuccess/paySuccess?orderId=' + orderId})

                          },
                          'fail':function (res) {

                          }
		                })
		                },
		                'fail':function(res){
                        console.log('支付失败',res)
		                }
	                })
                } else {
                    wx.hideToast();
                    toast.show(this, data.errormsg)
                }
            },
            complete: data => {
                // wx.hideToast();
            }
        });
    },
    onShareAppMessage: function () {
        return {
            title: '还在为违章处理排队烦恼吗？',
            desc: '邮证查办违章，免费查询，实时提醒，快速在线缴罚款，车主最贴心的车务管家！ ',
            path: 'pages/carInfo/list/list'
        }
    }
};
Page(pageData);